<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CancelOrderLog;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CancelOrderLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CancelOrderLog(['runErrand', 'order']), function (Grid $grid) {
            if (Admin::user()->school_id) {
                $grid->model()->whereHas('runErrand', function ($query) {
                    $query->where('school_id', Admin::user()->school_id);
                });
            }
            $grid->column('id')->sortable();
            $grid->column('runErrand.nick_name');
            $grid->column('runErrand.mobile');
            $grid->column('order.order_number');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('runErrand.nick_name')->width(2);
                $filter->like('runErrand.mobile')->width(2);
                $filter->like('order.order_number')->width(4);
            });
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CancelOrderLog(), function (Show $show) {
            $show->field('id');
            $show->field('run_errand_id');
            $show->field('order_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CancelOrderLog(), function (Form $form) {
            $form->display('id');
            $form->text('run_errand_id');
            $form->text('order_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
